<?php

namespace App\Http\Controllers\Admin\UserManager;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;
use Illuminate\Support\Facades\Validator;

class AvatarController extends BaseController {


    /**
     * @api {post} /user-manager/avatar/batch/check 头像审核-批量审核
     * @apiVersion 0.1.0
     * @apiName 会员管理-头像审核-批量审核
     * @apiGroup 会员管理
     *
     *
     * @apiParam {number} state                             2审核通过3审核失败4资质审核通过
     * @apiParam {number} remark                            封禁理由
     * @apiParam {number} ids                               操作的记录ids
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function check(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'state' => 'required|numeric|integer|in:2,3,4',
                "remark" => 'filled|string',
                "ids" => "required|string"
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $params = $request->input();

        if ($params['state'] == 3 && empty($params['remark'])) {
            throw new BasicException(10001, "理由不能为空！");
        }

        $res = app('repo_user');

        $rows = $res->batchCheckAvatar($params);
        if (!$rows) {
            throw new BasicException(10001, "设置失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {get} /user-manager/avatar 头像审核-列表
     * @apiVersion 0.1.0
     * @apiName 会员管理-头像审核-列表
     * @apiGroup 会员管理
     *
     *
     * @apiParam {number} user_code 用户编号 可选
     * @apiParam {string} nickname 昵称 可选
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     *
     *
     * @apiSuccessExample 成功响应
     *
     *
    {
    "code": 200,
    "data": {
        "list": [
            {
                "change_from(原头像)": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/android_images/639684/20200526183132_1590489092413483.jpg",
                "change_to(待审核头像)": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/android_images/639684/20200531200103_1590926463229796.jpg",
                "add_time(申请时间)": "2020-05-31 20:01:04",
                "remark(拒绝理由)": "",
                "update_time(最后修改时间)": "2020-09-02",
                "state(1待审核2审核通过3审核失败4资质审核通过)": 2,
                "nickname(昵称)": "Cc.茶衣招冠名",
                "user_code(用户编号)": 712440
            }
        ],
        "meta": {
                "page": 1,
                "limit": 10,
                "next": null,
                "total": 1
            }
        }
     }
     *
     *
     */
    public function list(Request $request) {
        //参数校验
//        try {
            $validator= Validator::make($request->all(), [
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
            ]);
            if ($validator->fails()) {
                throw new BasicException(10001, $validator->errors()->first());
            }
//        } catch (\Exception $e) {
//            throw new BasicException(10001, json_encode($e->errors()));
//        }

        $list = app('repo_user')->getAvatarQueryList($request->input());

        return Response::sendList($list);
    }

}
